//
//  LoginViewController.m
//  豆瓣zlw
//
//  Created by lanou3g on 16/1/9.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "LoginViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "RegisteredViewController.h"
@interface LoginViewController ()

@end

@implementation LoginViewController


-(void)viewWillAppear:(BOOL)animated
{
    
    [AVOSCloud setApplicationId:@"MsOHnv9Rb92SoX3Nsg7PtyU8-gzGzoHsz"
                      clientKey:@"LRnkEy80UsCHlaaprsehfX1E"];
    
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.HUD hide:YES];
    
   
}



- (IBAction)Button_Log_Action:(UIButton *)sender {
    // 通过 用户名 密码登录
    
    [AVUser logInWithUsernameInBackground:self.TextField_UserName.text    password:self.TextField_PassWord.text block:^(AVUser *user, NSError *error) {
        
        if (user != nil) {
            
            UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"登录成功" message:@"欢迎回来" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:vc animated:YES completion:^{
                sleep(3);
            }];
            
            vc.modalTransitionStyle=NO;
            
            
            
            
            [self dismissViewControllerAnimated:YES completion:^{
              
                
                
                NSUserDefaults * login = [NSUserDefaults standardUserDefaults];
                
                [login setBool:YES forKey:@"User"];
                [login synchronize];
                [self.navigationController popViewControllerAnimated:YES ];
                
                
            }];
            
            
        }else{
            
            UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"登录失败" message:@"请检查用户名和密码是否正确" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:vc animated:YES completion:^{
                sleep(3);
            }];
            
            vc.modalTransitionStyle=NO;
            
            [vc dismissViewControllerAnimated:YES completion:nil];
            
            
            
            
            
        }
        
        
        
    }];
    

}

- (IBAction)Button_Registr_Action:(UIButton *)sender {
    
    RegisteredViewController *VC=[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"MinRegin"];
    [self.navigationController pushViewController:VC animated:YES];
    
            
            

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
